package oop.controller.action.exam;

import oop.controller.action.DefaultAction;
import oop.data.Examination;
import oop.db.dao.ExaminationDAO;
import oop.db.dao.MarkDAO;

import com.oreilly.servlet.ParameterNotFoundException;

public class ViewAction extends DefaultAction {

	public static final int PAGE_LENGTH = 30;

	public ViewAction() {
	}

	@Override
	public void performImpl() throws Exception {
		String submit = request.getParameter("ev_submit");
		if ("deletemarks".equals(submit)) {
			try {
				String[] items = request.getParameterValues("ev_marks");
				int count = 0;
				for (String item : items) {
					long id = Long.parseLong(item);
					count += MarkDAO.drop(id);
				}
				message("Đã xóa " + count + " mục.");
			} catch (NumberFormatException ex) {
				error("ID không hợp lệ.");
			}
		}

		try {
			long examId = getParams().getLong("ev_id");
			Examination exam = ExaminationDAO.fetchById(examId);
			
			title(exam.getName());
			request.setAttribute("exam", exam);
		} catch (ParameterNotFoundException ex) {
			error("Bạn cần phải chọn bài kiểm tra.");
		} catch (NumberFormatException ex) {
			error("ID không hợp lệ.");
		}
	}

}
